/**
 * Created by 2006-1-19 17:15:15
 * $Id: TemplateUtil.java 9 2006-03-08 10:21:59Z wjx $
 */
package com.someok.utils.template;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;


/**
 * <p>
 * <a href="TemplateUtil.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author wjx
 * @version $Revision: 9 $ $Date: 2006-03-08 18:21:59 +0800 (星期三, 08 三月 2006) $
 */
public class TemplateUtil {
	public static String merge(TemplateContext context, String templateName) {

		Template template = null;
		StringWriter sw = new StringWriter();
		try {
			Velocity.init(initVelocity());

			template = Velocity.getTemplate(templateName);
			template.merge(context.getWrapContext(), sw);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return sw.toString();
	}

	private static Properties initVelocity() throws IOException {
		Properties prop = new Properties();
		prop.load(TemplateUtil.class
				.getResourceAsStream("/velocity.properties"));
		return prop;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TemplateContext ctx = new TemplateContext();
		ctx.put("aaa", "aaaaaaaaaaaaaaaa");
		ctx.put("bbb", "bbbbbbbbbbbbbb");
		String tmpName = "service/blog/comment.vm";
		System.out.println(TemplateUtil.merge(ctx, tmpName));

	}

}
